package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import client.CcrmApp;
import model.CostumerModel;
import model.CustomerReportModel;
import model.Envelope;
import view.*;

/**
 * This Class is a Costumer report Controller, which extends AbstractTransfer
 * @author jacob
 *
 */
public class CustomerReportController extends AbstractTransfer{
	private CustomerReportGui CustRepView;
	private MarketingManagController prevCont;
    private ArrayList<CustomerReportModel> listCostomer;
	/**
	 * 
	 * @param CustRepView is gui instance of Customer report
	 * @param prevC is previous controller instance  
	 */
	public CustomerReportController(CustomerReportGui CustRepView, MarketingManagController prevC){
		prevCont = prevC;
		this.CustRepView = CustRepView;
		
		this.CustRepView.addBackActionListener(new BackBottonListener());
		initReport();
	}
	
	public void initReport(){
		//send to server Envelop with ArrayList of CustomerReportModel 
		listCostomer = new ArrayList<CustomerReportModel>();
		Envelope ev = new Envelope(listCostomer, "Customer report");
		CcrmApp.clien.setCurrObj(this);
		sendToServer(ev);
	}
	/**
	 * Inner class that handles when Button back Pressed, implements ActiontListener
	 * @author jacob
	 *
	 */
	class BackBottonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			CustRepView.dispose();
			prevCont.MarkManGui.setVisible(true);		
		}
	}
	/**
	 * get previous controller
	 * @return MarketingManagController
	 */
	public MarketingManagController getPrevCont() {
		return prevCont;
	}
/**
 * set previous controller
 * @param prevCont
 */
	public void setPrevCont(MarketingManagController prevCont) {
		this.prevCont = prevCont;
	}
	/**
	 * get Customer  Report gui
	 * @return
	 */
	public CustomerReportGui getCustRepView() {
		return CustRepView;
	}
	/**
	 *  get List of Costumer
	 * @return  ArrayList<CustomerReportModel>
	 */
	public ArrayList<CustomerReportModel> getListCostomer() {
		return listCostomer;
	}

	@SuppressWarnings("unchecked")
	@Override
/*
 * (non-Javadoc)
 * @see controllers.AbstractTransfer#handleDBResult(java.lang.Object)
 */
	 public void handleDBResult(Object message) {
	     Envelope ev1 = (Envelope)message;
	     String str = new String("");
	     // set the report that come from server 
	    	 str+="Customer profiling report:\n\n";
	    		for(int i = 0; i < ((ArrayList<CustomerReportModel>)ev1.getObject()).size() ; i++){
					 str+= "Customer ID : " + (((ArrayList<CustomerReportModel>)ev1.getObject()).get(i).getC_id()) + ", Customer Name: "
					 		+ "  " + (((ArrayList<CustomerReportModel>)ev1.getObject()).get(i).getFirstName()) +"  "
					 				+ "" +(((ArrayList<CustomerReportModel>)ev1.getObject()).get(i).getLastName()) + ", \nquantity:"
					 						+ " "+(((ArrayList<CustomerReportModel>)ev1.getObject()).get(i).getQuintity())+ ", efficiency sales: "
					 								+ " " +(((ArrayList<CustomerReportModel>)ev1.getObject()).get(i).getEfficiency_sales()) +"%\n"  ; 

					 CustRepView.getTextArea().setText(str); 
	     }
	 }

}
